package de.jmda.mview.fx.node.typeshape.relation;

import java.util.Collection;
import java.util.LinkedList;

import de.jmda.fx.node.ArrowOrthogonal;
import de.jmda.mproc.RelationInfoCalculator.RelationInfoHas;
import de.jmda.mview.fx.node.typeshape.Typeshape;

/**
 * "Has" relations always start from a type's field and end at a type's shape.
 *
 * @author rwegner
 */
public class RelationHas
    extends Relation
{
	public RelationHas(
			RelationInfoHas relationInfo,
			Typeshape source,
			Typeshape target,
			Orthogonality orthogonality)
	{
		super(
				new RelationEndpointSourceFieldBased(
						relationInfo.getSource(),
						source.getFieldView(relationInfo.getSource())),
				new RelationEndpointTargetHas(target));
		
		// TODO store type shapes?

		this.orthogonality = orthogonality;
	}

	public RelationHas(
			RelationInfoHas relationInfo, Typeshape source, Typeshape target)
	{
		this(relationInfo, source, target, Orthogonality.OFF);
	}

	@Override
	public Collection<RelationSegment> getRelationSegments()
	{
		if (orthogonality == Orthogonality.OFF)
		{
			return super.getRelationSegments();
		}
		
		if (relationSegments == null)
		{
			// lazy initialisation
			relationSegments = new LinkedList<RelationSegment>();
		}
		
		ArrowOrthogonal arrowSource = source.getArrow();
		ArrowOrthogonal arrowTarget = target.getArrow();

		RelationSegment relationSegmentSourceTailToPoint =
				new RelationSegmentVertical(
						arrowSource.tailProperty(), arrowTarget.tailProperty());
		// TODO xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//		RelationSegment relationSegmentPointToTargetTail =
//				new RelationSegmentHorizontal(
//						arrowTarget.tailProperty(), arrowSource.tailProperty());
		RelationSegment relationSegmentPointToTargetTail =
				new RelationSegmentHorizontalToEndpoint(
						relationSegmentSourceTailToPoint.endPointProperty(),
						arrowTarget.tailProperty(),
						target);

		relationSegmentSourceTailToPoint.activateHighlighting();
		relationSegmentSourceTailToPoint.activateDragging();

		relationSegmentPointToTargetTail.activateHighlighting();
		relationSegmentPointToTargetTail.activateDragging();

		relationSegments.add(relationSegmentSourceTailToPoint);
		relationSegments.add(relationSegmentPointToTargetTail);

		return relationSegments;
	}
}